/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2022-07-18
 * Time: 23:39
 */
//合并两个有序链表
public class MergeTwoSortedLinkedList {
    public static class ListNode {
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }
    public static ListNode mergeTwoLists(ListNode head1, ListNode head2) {
        if (head1 == null || head2 == null) {
            return head1 == null ? head2 : head1;
        }
        //新建头结点
        ListNode head=new ListNode( -1 );
        ListNode cur=head;
        while (head1 != null && head2 != null) {
            if (head1.val <= head2.val) {
                cur.next = head1;
                head1 = head1.next;
            } else {
                cur.next = head2;
                head2 = head2.next;
            }
            cur = cur.next;
        }
        cur.next = head1 != null ? head1 : head2;
        return head.next;
    }
}
